﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using HYView;

namespace HYRElectric.WirePlace
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdModifyConduitLevel : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Selection selection = activeUIDocument.Selection;
			Document document = activeUIDocument.Document;
			Transaction transaction = new Transaction(document, "ModifyConduitLevel");
			Result result2;
			try
			{
                Autodesk.Revit.DB.View activeView = cmdData.Application.ActiveUIDocument.Document.ActiveView;
                if (activeView.ViewType != (ViewType)1 && activeView.ViewType != (ViewType)2 && activeView.ViewType != (ViewType)4 && activeView.ViewType != (ViewType)3 && activeView.ViewType != (ViewType)117)
				{
					Result result = SwitchView.SetUsableView((CmdViewType)115, cmdData);
					if (result != Result.Succeeded)
					{
						return result;
					}
				}
				transaction.Start();
				ElementSet elementSet = new ElementSet();
				IList<Element> list = new FilteredElementCollector(activeUIDocument.ActiveView.Document).OfClass(typeof(Wire)).OfCategory(BuiltInCategory.OST_Wire).ToElements();
				if (list.Count > 0)
				{
					foreach (Element element in list)
					{
						elementSet.Insert(element);
					}
					document.ActiveView.HideYJKElements(elementSet);
					document.Regenerate();
				}
				Category category = activeUIDocument.Document.Settings.Categories.get_Item(BuiltInCategory.OST_Conduit);
				document.ActiveView.SetYJKVisibility(category, true);
				IList<Reference> list2 = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new ConduitFilter(), "请选择要修改标高的线管");
				if (list2.Count <= 0)
				{
					MessageBox.Show("选择的对象里没有线管，请重新选择。", "盈建科提示");
					transaction.RollBack();
					result2 = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					List<Conduit> list3 = new List<Conduit>();
					double num = 0.0;
					foreach (Reference reference in list2)
					{
						Conduit conduit = activeUIDocument.Document.GetElement(reference) as Conduit;
						LocationCurve locationCurve = conduit.Location as LocationCurve;
						XYZ hyendPoint = locationCurve.Curve.GetEndPoint(0);
						XYZ hyendPoint2 = locationCurve.Curve.GetEndPoint(1);
						if (Math.Abs(hyendPoint.X - hyendPoint2.X) >= this.js || Math.Abs(hyendPoint.Y - hyendPoint2.Y) >= this.js || Math.Abs(hyendPoint.Z - hyendPoint2.Z) <= this.js)
						{
							double num2 = AssistFunc.feetToMM(conduit.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble());
							if (num2 > num)
							{
								num = num2;
							}
							list3.Add(conduit);
						}
					}
					using (DlgModifyConduitLevel dlgModifyConduitLevel = new DlgModifyConduitLevel(num))
					{
						DialogResult dialogResult = dlgModifyConduitLevel.ShowDialog();
						if (dialogResult == DialogResult.Cancel)
						{
							transaction.RollBack();
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (dialogResult == DialogResult.OK)
						{
							double num3 = AssistFunc.mmToFeet(Convert.ToDouble(dlgModifyConduitLevel.txtHeight.Text));
							foreach (Element element2 in list3)
							{
								element2.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).Set(num3);
								RevitVersionFuncs.AddToSelection(selection, element2);
							}
						}
					}
					if (elementSet.Size > 0)
					{
						document.ActiveView.UnHideYJKElements(elementSet);
					}
					transaction.Commit();
					result2 = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception)
			{
				transaction.RollBack();
				result2 = Autodesk.Revit.UI.Result.Failed;
			}
			return result2;
		}

		private double js = 0.01;
	}
}
